package com.qt.myio;

import lombok.SneakyThrows;

import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author zsf
 * @Date 2023/9/26 23:06
 * @Version 1.0
 */
public class BufferedStreamDemo02 {

    public static void main(String[] args) throws IOException {

        /**
         * 字节缓冲流
         *
         * 恢复文件顺序
         */
        BufferedReader br = new BufferedReader(new FileReader("aaa/文件顺序.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("aaa/文件顺序copy.txt",true));

        String len;
        HashMap<Integer, String> res = new HashMap<>();
        while ((len = br.readLine()) != null) {
            res.put(Integer.valueOf(len.split("\\.")[0]), len);
        }

        res.entrySet().stream()
                .sorted((o1, o2) -> o1.getKey() - o2.getKey())
                .collect(Collectors.toList())
                .stream()
                .forEach(item -> {
                    try {
                        bw.write(item.getValue());
                        bw.newLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });

        bw.close();
        br.close();



    }

}
